package cn.iocoder.yudao.module.forward.http.register;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
import cn.iocoder.yudao.module.forward.http.register.model.Authorize;
import cn.iocoder.yudao.module.forward.http.register.model.AuthorizeResp;
import cn.iocoder.yudao.module.forward.http.register.model.RegisterInfo;
import cn.iocoder.yudao.module.forward.http.register.model.RegisterResp;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;

@Slf4j
public class RegisterUtil {
    public static final String URL_REGISTER = "/register/tm_register";
    public static final String URL_AUTH = "/register/tm_authorize";

    public static RegisterResp register(String url, RegisterInfo registerInfo) {
//        StringBuilder stringBuilder = new StringBuilder(url + URL_REGISTER);
//        stringBuilder.append("?");
//        stringBuilder.append("license=");
//        stringBuilder.append(registerInfo.getLicense());
//        stringBuilder.append("&");
//        stringBuilder.append("owner=");
//        stringBuilder.append(registerInfo.getOwner());
//        stringBuilder.append("&");
//        stringBuilder.append("type=");
//        stringBuilder.append(registerInfo.getType());
//        log.info("开始注册:{}", stringBuilder);
//        String ret = HttpUtils.get(stringBuilder.toString(), null);
        log.info("开始注册:{}", registerInfo.getParams());
        String ret = post(url + URL_REGISTER, registerInfo.getParams());
        log.info("注册结果:{}", ret);
        RegisterResp registerResult = null;
        try {
            registerResult = JsonUtils.parseObject2(ret, RegisterResp.class);
        }catch (Exception e){}
        return registerResult;
    }

    public static AuthorizeResp auth(String url, Authorize authorize) {
//        StringBuilder stringBuilder = new StringBuilder(url + URL_AUTH);
//        stringBuilder.append("?");
//        stringBuilder.append("scope=");
//        stringBuilder.append(authorize.getScope());
//        stringBuilder.append("&");
//        stringBuilder.append("clientId=");
//        stringBuilder.append(authorize.getClientId());
//        stringBuilder.append("&");
//        stringBuilder.append("uname=");
//        stringBuilder.append(authorize.getUname());
//        stringBuilder.append("&");
//        stringBuilder.append("pwd=");
//        stringBuilder.append(authorize.getPwd());
//        log.info("开始授权:{}", stringBuilder);
        log.info("开始授权:{}", authorize.getParams());
        String ret = post(url + URL_AUTH, authorize.getParams());
        log.info("授权结果:{}", ret);
        AuthorizeResp authorizeResp = null;
        try {
            authorizeResp = JsonUtils.parseObject2(ret, AuthorizeResp.class);
        }catch (Exception e){}
        return authorizeResp;
    }

    public static String post(String url, Map<String, Object> maps) {
        try (HttpResponse response = HttpRequest.post(url)
                .form(maps)
                .header("Content-Type", "application/x-www-form-urlencoded")
                .execute()) {
            return response.body();
        }
    }
}
